package com.chuangke.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@Configuration
public class FastjsonConfig implements WebMvcConfigurer {

	@SuppressWarnings("deprecation")
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		/** 1.需要定义一个Convert转换消息的对象 */
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

		/** 2.添加fastjson的配置信息，比如是否要格式化返回的json数据 */
		FastJsonConfig fastJsonConfig = new FastJsonConfig();

		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteNullNumberAsZero,
				SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);

		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");// 和SerializerFeature.WriteDateUseDateFormat保留一个


		/** 3.在convert中添加配置信息 */
		fastConverter.setFastJsonConfig(fastJsonConfig);

		/** 4.中文乱码 */
		List<MediaType> fastMediaTypes = new ArrayList<>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		fastConverter.setSupportedMediaTypes(fastMediaTypes);

		return new HttpMessageConverters(fastConverter);

	}
}
